package cm.com.carlamanda.service.impl;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.springframework.stereotype.Service;

import cm.com.carlamanda.constant.Regex;
import cm.com.carlamanda.service.HttpService;
import cm.com.carlamanda.util.StringUtil;

@Service
public class HttpServiceImpl implements HttpService{
    /**
     * ����js�ļ��е�ĳһ���������
     * 
     * @param code ��Ҫ��������
     * @param JsURL js�ļ���url  
     * @param functionName ���õ�js�ļ��еĺ�����
     *        
     * @return newCode   ��js�ļ��еĺ��������µ����
     * */
    public  String processDataViaJs(String code,String JsURL,String functionName ) throws Exception {
        ScriptEngineManager manager = new ScriptEngineManager();
        String newCode = "";
        InputStreamReader inputStreamReader = null;
        ScriptEngine engine = manager.getEngineByName("javascript");
        try {
            URL url = new URL(JsURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("Content-Type", "text/html");
            inputStreamReader = new InputStreamReader(getInputContent("GET",conn));//��ȡjs����
        } catch (ConnectException ce) {
            ce.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        engine.eval(inputStreamReader);
        if (engine instanceof Invocable) {
            Invocable invoke = (Invocable) engine; 
            newCode = (String) invoke.invokeFunction(functionName, code);// ���ö�Ӧ������������
                                                                         //1������:��Ҫ����� �ַ��
       }
        inputStreamReader.close();
        return newCode;
    }
    public  String getInputContent(String jsUrl) throws Exception  {
       String jsContent= "";
        BufferedReader bufferdeReader = null;
        try {
            URL url = new URL(jsUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("Content-Type", "text/html");
            
            bufferdeReader = new BufferedReader(new InputStreamReader(getInputContent("GET",conn)));//��ȡjs����
            String s = "";
            while((s =bufferdeReader.readLine()) !=null ){
                jsContent+=s+"\n";
            }
        } catch (ConnectException ce) {
            ce.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsContent;
    }
    
    private  InputStream getInputContent(String type, HttpURLConnection conn)  throws Exception{
        conn.setRequestProperty("ContentEncoding", "UTF-8");
        conn.connect();
        //4.��ȡͷ����Ϣ֮һ����ȡ����ͷ����Ϣ���ٱ���  
        Map<String, List<String>> headers = conn.getHeaderFields();  
        for(Map.Entry<String,List<String>> entry : headers.entrySet()){  
            System.out.print(entry.getKey()+" : ");  
            for(String value : entry.getValue()){  
                System.out.println(value+" , ");  
            }  
        }
   
        //4.��ȡͷ����Ϣ֮����ʹ�ü�㷽��  
        System.out.println("----------");  
        System.out.println("getContentType: " + conn.getContentType());  
        System.out.println("getContentLength: " + conn.getContentLength());  
        System.out.println("getContentEncoding: " + conn.getContentEncoding());  
        System.out.println("getDate: " + conn.getDate());  
        System.out.println("getExpiration: " + conn.getExpiration());  
        System.out.println("getLastModifed: " + conn.getLastModified());  
        System.out.println("----------");  
        InputStream is = conn.getInputStream();  
        return is;  
    }
    @Override
    public List<String> getJSLinks(String url) throws Exception {
        String html = getInputContent(url);
        List<String> jslinks = StringUtil.regexString(html, Regex.RGX_JAVASCRIPT_URL);
        return jslinks;
    }
}
